package main

import (
	"fmt"
	"math"
	"path/filepath"

	"demserver/src/elevation"
)

func main() {
	// Use the actual GeoTIFF file
	filename := "./demdata/scdjy.tif"

	// Get absolute path to the file
	absPath, err := filepath.Abs(filename)
	if err != nil {
		fmt.Printf("Failed to get absolute path: %v\n", err)
		return
	}

	fmt.Printf("Loading GeoTIFF file: %s\n", absPath)
	geoTiff, err := elevation.LoadGeoTIFF(absPath)
	if err != nil {
		fmt.Printf("Failed to load GeoTIFF: %v\n", err)
		return
	}
	fmt.Println("GeoTIFF loaded successfully")

	// Test cases with known expected values
	testCases := []struct {
		lat      float64
		lon      float64
		expected float64
		desc     string
	}{
		{31.03021050140226, 103.55147461467699, 837.6951116196124, "High precision test case"},
		// 可以添加更多测试用例
	}

	for _, tc := range testCases {
		fmt.Printf("\n=== %s ===\n", tc.desc)
		fmt.Printf("Input coordinates:\n")
		fmt.Printf("  Lat: %.14f\n", tc.lat)
		fmt.Printf("  Lon: %.14f\n", tc.lon)

		x, y, err := geoTiff.LatLonToPixel(tc.lat, tc.lon)
		if err != nil {
			fmt.Printf("Failed to convert coordinates: %v\n", err)
			continue
		}

		fmt.Printf("Pixel coordinates:\n")
		fmt.Printf("  X: %.14f\n", x)
		fmt.Printf("  Y: %.14f\n", y)

		// Get elevation value
		elevationVal, err := geoTiff.GetElevation(x, y)
		if err != nil {
			fmt.Printf("Failed to get elevation: %v\n", err)
			continue
		}

		fmt.Printf("Elevation results:\n")
		fmt.Printf("  Expected: %.14f\n", tc.expected)
		fmt.Printf("  Actual: %.14f\n", elevationVal)
		fmt.Printf("  Difference: %.14f\n", elevationVal-tc.expected)

		// Detailed analysis
		x0 := int(math.Floor(x))
		y0 := int(math.Floor(y))
		x1 := x0 + 1
		y1 := y0 + 1

		fmt.Printf("\nSurrounding pixels:\n")
		fmt.Printf("  x0: %d, y0: %d\n", x0, y0)
		fmt.Printf("  x1: %d, y1: %d\n", x1, y1)

		// Get pixel values
		q00, _ := geoTiff.GetElevation(float64(x0), float64(y0))
		q01, _ := geoTiff.GetElevation(float64(x0), float64(y1))
		q10, _ := geoTiff.GetElevation(float64(x1), float64(y0))
		q11, _ := geoTiff.GetElevation(float64(x1), float64(y1))

		fmt.Printf("Pixel values:\n")
		fmt.Printf("  q00 (%d, %d): %.14f\n", x0, y0, q00)
		fmt.Printf("  q01 (%d, %d): %.14f\n", x0, y1, q01)
		fmt.Printf("  q10 (%d, %d): %.14f\n", x1, y0, q10)
		fmt.Printf("  q11 (%d, %d): %.14f\n", x1, y1, q11)

		// Interpolation weights
		dx := x - math.Floor(x)
		dy := y - math.Floor(y)

		fmt.Printf("Interpolation weights:\n")
		fmt.Printf("  dx: %.14f\n", dx)
		fmt.Printf("  dy: %.14f\n", dy)

		// Manual calculation
		f1 := (1 - dx) * (1 - dy) * q00
		f2 := dx * (1 - dy) * q10
		f3 := (1 - dx) * dy * q01
		f4 := dx * dy * q11
		result := f1 + f2 + f3 + f4

		fmt.Printf("Manual bilinear interpolation:\n")
		fmt.Printf("  f1: %.14f\n", f1)
		fmt.Printf("  f2: %.14f\n", f2)
		fmt.Printf("  f3: %.14f\n", f3)
		fmt.Printf("  f4: %.14f\n", f4)
		fmt.Printf("  result: %.14f\n", result)
		fmt.Printf("  difference from expected: %.14f\n", result-tc.expected)
	}
}
